from utlis.db import DB
import logging
from jsonschema import validate

logger = logging.getLogger(__name__)


def get_file():
    return open(file=r"Users\26524\Desktop\baiyanghui.png", mode='rb')


def clean_test_data(username):
    """测试前清理数据"""
    with DB() as db:
        db.execute("DELETE FROM y_user WHERE username = %s", (username,))


def database_validate(sql, params=None):
    """直接返回查询结果的整数值"""
    try:
        with DB() as db:
            result = db.query_value(sql, params or ())
            logger.debug(f"数据库验证结果: sql={sql}, result={result}")
            return int(result) if result is not None else 0
    except Exception as e:
        logger.error(f"数据库验证异常: {str(e)}")
        return 0


def query_database(sql, params=None):
    """查询数据库"""
    with DB() as db:
        return db.query(sql, params)


def verify_db_record(sql, expected_value, params=None):
    """验证数据库记录"""
    with DB() as db:
        result = db.query_value(sql, params)
        return str(result) == str(expected_value) if result is not None else False


def get_db_value(sql, params=None):
    """获取单个数据库值"""
    with DB() as db:
        return db.query_value(sql, params)


def a():
    result = database_validate('SELECT COUNT(*) FROM y_user WHERE username = %s', ['byhad01'])
    print(f"结果: {result}, 类型: {type(result)}")  # 应输出: 结果: 1, 类型: <class 'int'>
    return result
